package com.inspectortime.service.search.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.util.StringUtils;

import com.inspectortime.domain.InspectorCompany;
import com.inspectortime.service.search.InspectorSearchDao;

/**
 * Hibernate implementation of InspectorSearchDao
 * 
 * @author bones
 *
 */
public class InspectorSearchDaoImpl implements InspectorSearchDao {
	
	private SessionFactory sessionFactory;
	
	public InspectorSearchDaoImpl(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.inspectortime.service.search.InspectorSearchDao#searchInspectorCompanyByName(java.lang.String)
	 */
	public List<InspectorCompany> searchInspectorCompanyByName(String companyName) {
		Session session = SessionFactoryUtils.getSession(this.sessionFactory, true);
		Criteria crit = session.createCriteria(InspectorCompany.class);
		crit.add(Restrictions.ilike("name", companyName, MatchMode.ANYWHERE));
		return crit.list();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.inspectortime.service.search.InspectorSearchDao#searchInspectorCompanyByInspectorName(java.lang.String, java.lang.String)
	 */
	public List<InspectorCompany> searchInspectorCompanyByInspectorName(String firstName, String lastName) {
		Session session = SessionFactoryUtils.getSession(this.sessionFactory, true);
		Criteria crit = session.createCriteria(InspectorCompany.class);
		crit.createCriteria("inspectors", "inspector");
		if (StringUtils.hasText(firstName)) {
			crit.add( Restrictions.ilike("inspector.name.first", firstName, MatchMode.ANYWHERE));
		}
		if (StringUtils.hasText(lastName)) {
			crit.add( Restrictions.ilike("inspector.name.last", lastName, MatchMode.ANYWHERE) );
		}
		return crit.list();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.inspectortime.service.search.InspectorSearchDao#searchInspectorCompanyByRealtorName(java.lang.String, java.lang.String)
	 */
	public List<InspectorCompany> searchInspectorCompanyByRealtorName(String firstName, String lastName) {
		Session session = SessionFactoryUtils.getSession(this.sessionFactory, true);
		Criteria crit = session.createCriteria(InspectorCompany.class);
		crit.createCriteria("referringAgents", "referringAgent");
		if (StringUtils.hasText(firstName)) {
			crit.add( Restrictions.ilike("referringAgent.name.first", firstName, MatchMode.ANYWHERE) );
		}
		if (StringUtils.hasText(lastName)) {
			crit.add( Restrictions.ilike("referringAgent.name.last", lastName, MatchMode.ANYWHERE));
		}
		return crit.list();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.inspectortime.service.search.InspectorSearchDao#searchInspectorCompanyByRealtorCompanyName(java.lang.String)
	 */
	public List<InspectorCompany> searchInspectorCompanyByRealtorCompanyName(String companyName) {
		Session session = SessionFactoryUtils.getSession(this.sessionFactory, true);
		Criteria crit = session.createCriteria(InspectorCompany.class);
		crit.createCriteria("referringAgents", "referringAgent");
		crit.add( Restrictions.ilike("referringAgent.company", companyName, MatchMode.ANYWHERE) );
		return crit.list();
		
	}

}
